﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Resources;
using ZaidimasSandelys.Models.Classes;
using ZaidimasSandelys.Models.Interfaces;
using Ninject;
using ZaidimasSandelys.Properties;

namespace ZaidimasSandelys
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private static readonly string CONN_STRING_KEY = "ZaidimasSandelys.Properties.Settings.ZaidimasSandelysConnectionString";
        private static readonly string MP3_NAME = "Mp3File";

        /// <summary>
        /// Unity container must follow singleton pattern
        /// </summary>
        private static StandardKernel _kernel = null;
        public static StandardKernel Kernel
        {
            get
            {
                if (_kernel == null)
                    ConfigureNinject();
                return _kernel;
            }
        }


        private void CreateContainer(object sender, StartupEventArgs e)
        {
            var mainWindow = Kernel.Get<MainWindow>();
            mainWindow.Show();
        }

        private static void ConfigureNinject()
        {
            _kernel = new StandardKernel();
            string sqlProcessor = Settings.Default.sqlProcessor;
            string mp3 = Settings.Default.Mp3File;

            var connString = ConfigurationManager.ConnectionStrings[CONN_STRING_KEY].ConnectionString;
            if (sqlProcessor == "ADO")
            {
                _kernel.Bind<IAppData>().To<AppSqlCeData>().InSingletonScope().WithConstructorArgument("connString", connString);
            }
            else
                _kernel.Bind<IAppData>().To<AppSqlCeDataEF>().InSingletonScope().WithConstructorArgument("connString", String.Empty);
            var appData = _kernel.Get<IAppData>();

            _kernel.Bind<ICurrentGame>().To<CurrentGame>().WithConstructorArgument("appData", appData);
            _kernel.Bind<IMainWindowModel>().To<MainWindowModel>()
                .WithConstructorArgument("appData", appData)
                .WithConstructorArgument("mp3FileName", mp3);
            _kernel.Bind<MainWindow>().ToSelf();

        }
    }
}
